<template>
    <el-dialog v-model="show" title="添加退货原因" width="400px"  @close="quxiao">
        <el-form ref="formRef" :model="form" :rules="rules">
            <el-form-item prop="name" label="原因类型:" label-width="100px">
                <el-input v-model="form.name" autocomplete="off" />
            </el-form-item>
            <el-form-item prop="sort" label="排序:" label-width="100px">
                <el-input v-model="form.sort" autocomplete="off" />
            </el-form-item>
            <el-form-item prop="status" label="是否启用" label-width="100px">
                <el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="quxiao">取消</el-button>
                <el-button type="primary" @click="sureFn"> 确定 </el-button>
            </span>
            </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { user } from '@/api';
import { ElMessage, FormInstance } from 'element-plus';
import { emit } from 'process';
import { reactive, ref, watch } from 'vue';

const emit = defineEmits(['hide','refresh'])
const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
  id: {
    type: Number,
    default: 0,
  },
});

const form = reactive({
    name: '',
    sort: 0,
    status: 0,
})
// 封装清空
const qingkong=()=>{
    form.name = '';
    form.sort = 0;
    form.status = 0;
}
const rules = {
    name: { required: true, message: "请输入原因类型", trigger: "blur" }
}
const formRef = ref<FormInstance>();
// 确定按钮
const sureFn=()=>{
    if(props.id === 0){
        // id为0是添加，否则是编辑
        formRef.value?.validate((valid: boolean) => {
            if(valid){
                user.returnReasonCreateApi(form).then(res=>{
                    if(res.code){
                        // 提示用户
                        ElMessage.success("添加成功");
                        // 关闭 dialog
                        emit("hide");
                        // 刷新表格
                        emit("refresh");
                        // 清空数据
                        qingkong()
                    }
                })
            }
        })
    }else{
        user.updateReturnReasonApi(form).then(res=>{
            if(res.code === 200){
                // 提示用户
                ElMessage.success("修改成功");
                // 关闭 dialog
                emit("hide");
                // 刷新表格
                emit("refresh");
                // 清空数据
                qingkong()
            }
        })
    }
}
// 取消按钮
const quxiao=()=>{
    emit('hide');
    qingkong()
}
watch(
  () => props.id,
  () => {
    if (props.id === 0) {
      formRef.value?.resetFields();
      return;
    } else {
      // 否则就是点击的编辑，就可以向服务器发请求
      user.getReturnReasonApi(props.id).then((res) => {
        if (res.code === 200) {
          Object.assign(form, res.data); // api 没见过，就去看文档， Object.assign() ---> MDN
        }
      });
    }
  }
);
</script>

<style lang="less" scoped>

</style>